{include file="public/layout" /}
<style>
    .layui-form-label{width: 150px !important;}
    .layui-word-aux{margin-left: 150px !important;}
</style>
<body class="laytp-container">
<div class="layui-card" >
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <input type="hidden" name="info[id]" value="{$info.id}" class="layui-input">
            <div class="layui-form-item">
                <label class="layui-form-label">优惠券类型：</label>
                <div class="layui-input-inline" style="width:600px">
                    <input type="radio" name="info[type]" value="1" title="代金券" {if !$info || $info['type'] == 1}checked{/if} lay-filter="coupon_type">
                    <input type="radio" name="info[type]" value="2" title="折扣券" {if $info && $info['type'] == 2}checked{/if} lay-filter="coupon_type">
                </div>
                <div class="layui-word-aux layui-clear">代金券：可以直接抵扣金额使用；折扣券：用于折扣商品加个百分比</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">优惠券名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="info[name]" value="{$info.name}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="coupon_type1">
                <div class="layui-form-item coupon_type1_1" {if $info && $info['type']==2}style="display:none"{/if}>
                    <label class="layui-form-label">优惠金额：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="info[money]" value="{$info.money}" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">元</div>
                    <div class="layui-word-aux layui-clear">注：用于商品订单抵扣时，优惠券只能抵消商品金额，不能抵消运费</div>
                </div>
                <div class="layui-form-item coupon_type1_10" {if !$info || $info['type']!=2}style="display:none"{/if} >
                    <label class="layui-form-label">折扣比例：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="info[discount]" value="{$info.discount}" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">%</div>
                    <div class="layui-word-aux" style="clear:both;margin-left:125px">例如9折优惠券则填写90</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">最低消费金额：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="info[minprice]" value="{$info.minprice|default=0}" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">元</div>
                    <div class="layui-word-aux" style="clear:both;margin-left:125px"> 注：购物金额（不含运费）达到最低消费金额才可使用优惠券，无门槛优惠券请填0</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">适用范围：</label>
                    <div class="layui-input-inline" style="width:500px">
                        <input type="radio" name="info[fwtype]" value="0" {if !$info || $info['fwtype']==0}checked{/if} title="所有商品" lay-filter="fwtype"/>
                        <input type="radio" id="fwtype1" name="info[fwtype]" value="1" {if $info && $info['fwtype']==1}checked{/if} title="指定类目" lay-filter="fwtype"/>
                        <input type="radio" id="fwtype2" name="info[fwtype]" value="2" {if $info && $info['fwtype']==2}checked{/if} title="指定商品" lay-filter="fwtype"/>
                    </div>
                    <div class="fwtype1 fwtypeExtend" style="clear:both;{if !$info || $info['fwtype']!=1}display:none{/if}">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline" style="width:auto">
                            <table id="setcategorydiv"  class="layui-table" style="width:500px">
                                <thead>
                                <tr>
                                    <th>分类ID</th>
                                    <th>分类名称</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                {if !empty($categorydata)}
                                {foreach $categorydata as $k=>$ff}
                                <tr class="categorylisttr">
                                    <td>{$ff.id}</td>
                                    <td>{$ff.name}</td>
                                    <td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delCategory(this,{$ff['id']})">删除</button></td>
                                </tr>
                                {/foreach}
                                {/if}
                                <tr id="categoryaddtr">
                                    <td colspan="3"><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showChooseCategory()">添加</button></td>
                                </tr>
                            </table>
                            <input type="hidden" name="info[categoryids]" value="{$info.categoryids}"/>
                        </div>
                    </div>
                    <script>
                        var chooseCategoryLayer;
                        function showChooseCategory(){
                            chooseCategoryLayer = layer.open({type:2,title:'选择商品分类',content:"{:url('GoodsCategory/choosecategory')}",area:['1000px','600px'],shadeClose:true});
                        }
                        function chooseCategory(fid,fname){
                            layer.close(chooseCategoryLayer);
                            var categoryids = [];
                            var isadd = 0;
                            $('.categorylisttr').each(function(){
                                var thisfid = $(this).find('td:eq(0)').html();
                                if(thisfid == fid){
                                    isadd = 1
                                    dialog('该分类已添加过了');
                                }
                                categoryids.push(thisfid)
                            })
                            if(isadd == 0){
                                categoryids.push(fid)
                                $("input[name='info[categoryids]']").val(categoryids.join(','));
                                $('#categoryaddtr').before('<tr class="categorylisttr"><td>'+fid+'</td><td>'+fname+'</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delCategory(this,'+fid+')">删除</button></td></tr>');
                            }
                            console.log(categoryids)
                        }
                        function delCategory(obj,fid){
                            $(obj).parent().parent().remove();
                            var categoryids = [];
                            $('.categorylisttr').each(function(){
                                categoryids.push($(this).find('td:eq(0)').html())
                            })
                            $("input[name='info[categoryids]']").val(categoryids.join(','));
                        }
                    </script>
                    <div class="fwtype2 fwtypeExtend" style="clear:both;{if !$info || $info['fwtype']!=2}display:none{/if}">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline" style="width:auto">
                            <table id="setproductdiv"  class="layui-table" style="width:500px">
                                <thead>
                                <tr>
                                    <th>商品ID</th>
                                    <th>商品名称</th>
                                    <th>操作</th>
                                </tr>
                                </thead>

                                {foreach $productdata as $k=>$ff}
                                <tr class="productlisttr"><td>{$ff.id}</td><td>{$ff.name}</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delProduct(this,{$ff.id})">删除</button></td></tr>
                                {/foreach}
                                <tr id="productaddtr">
                                    <td colspan="3"><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showChooseProduct()">添加</button></td>
                                </tr>
                            </table>
                            <input type="hidden" name="info[productids]" value="{$info.productids}"/>
                        </div>
                    </div>
                    <script>
                        var chooseProductLayer;
                        function showChooseProduct(){
                            chooseProductLayer = layer.open({type:2,title:'选择商品',content:"/admin/Goods/choosegoods/hidebid/1",area:['1000px','600px'],shadeClose:true});
                        }
                        function choosegoods(res){
                            console.log(res);
                            var product = res
                            layer.close(chooseProductLayer);
                            var productids = [];
                            var isadd = 0;
                            $('.productlisttr').each(function(){
                                var thisfid = $(this).find('td:eq(0)').html();
                                if(thisfid == product.goods_id){
                                    isadd = 1
                                    dialog('该商品已添加过了');
                                }
                                productids.push(thisfid)
                            })
                            if(isadd == 0){
                                productids.push(product.goods_id)
                                $("input[name='info[productids]']").val(productids.join(','));
                                $('#productaddtr').before('<tr class="productlisttr"><td>'+product.goods_id+'</td><td>'+product.goods_name+'</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delProduct(this,'+product.goods_id+')">删除</button></td></tr>');
                            }
                            console.log(productids)
                        }
                        function delProduct(obj,fid){
                            $(obj).parent().parent().remove();
                            var productids = [];
                            $('.productlisttr').each(function(){
                                productids.push($(this).find('td:eq(0)').html())
                            })
                            $("input[name='info[productids]']").val(productids.join(','));
                        }
                    </script>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">使用说明：</label>
                <div class="layui-input-inline" style="width:300px">
                    <textarea name="info[usetips]" class="layui-textarea">{$info.usetips}</textarea>
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="coupon_yxqtime" {if $info && $info['type']==20}style="display:none"{/if}>
                <div class="layui-form-item">
                    <label class="layui-form-label">有效期：</label>
                    <div class="layui-input-inline" style="width:500px">
                        <input type="radio" name="info[yxqtype]" value="1" title="固定时间范围" {if !$info || $info['yxqtype']==1}checked{/if} lay-filter="yxqtype"/>
                        <input type="radio" name="info[yxqtype]" value="2" title="领取后时长" {if $info && $info['yxqtype']==2}checked{/if} lay-filter="yxqtype"/>
                    </div>
                </div>
                <div class="layui-form-item yxqtimeitem" id="yxqtimeitem_1" {if $info && $info['yxqtype']!=1}style="display:none"{/if}>
                    <label class="layui-form-label"></label>
                    <div class="layui-form-mid">有效期时间</div>
                    <div class="layui-input-inline" style="width:300px">
                        <input type="text" name="info[yxqtime]" value="{$info.yxqtime}" id="yxqtime" autocomplete="off" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-form-item yxqtimeitem" id="yxqtimeitem_2" {if $info && $info['yxqtype']!=2}style="display:none"{/if}>
                    <label class="layui-form-label"></label>
                    <div class="layui-form-mid">领取后</div>
                    <div class="layui-input-inline" style="width:70px">
                        <input type="text" name="yxqdate2" value="{$info.yxqdate}" class="layui-input"/>
                    </div>
                    <div class="layui-form-mid">天内有效</div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">领取条件：</label>
                    <div class="layui-input-inline" style="width:600px">
                        <input type="checkbox" name="info[gettj][]" value="-1" title="所有人" {if !$info || in_array('-1',$info['gettj'])}checked{/if}/>
                        {foreach $memberlevel as $v}
                        <input type="checkbox" name="info[gettj][]" value="{$v.rank}" title="{$v.level_name}" {if $info && in_array($v['rank'],$info['gettj'])}checked{/if}/>
                        {/foreach}
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">所需金额：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="info[price]" value="{$info.price}"  lay-verType="tips" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">需要消耗多少钱购买</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">所需{:t('积分')}：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="info[score]" value="{$info.score}"  lay-verType="tips" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">需要消耗多少{:t('积分')}兑换，当设置需要花钱购买时此项不生效</div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">库存：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="info[stock]" value="{$info.stock|default=100}" class="layui-input">
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label">每人可领取数：</label>
                <div class="layui-input-inline">
                    <input type="text" name="info[perlimit]" value="{$info['perlimit']}" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">每人最多可领取多少张</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">开始时间：</label>
                <div class="layui-input-inline">
                    <input type="text" name="info[starttime]" autocomplete="off" value="{$info.starttime}" lay-verify="required" lay-verType="tips" class="layui-input" id="starttime">
                </div>
                <div class="layui-form-mid layui-word-aux">活动开始时间</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">结束时间：</label>
                <div class="layui-input-inline">
                    <input type="text" name="info[endtime]" autocomplete="off" value="{$info.endtime}" lay-verify="required" lay-verType="tips" class="layui-input" id="endtime">
                </div>
                <div class="layui-form-mid layui-word-aux">活动结束时间</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">序号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="info[sort]" value="{$info.sort}" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="margin-left:10px;">用于排序,越大越靠前</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">领券中心显示：</label>
                <div class="layui-input-inline">
                    <input type="radio" name="info[tolist]" value="1" {if !$info || $info['tolist']==1}checked{/if} title="是" lay-filter="showtj"/>
                    <input type="radio" name="info[tolist]" value="0" {if $info && $info['tolist']==0}checked{/if} title="否" lay-filter="showtj"/>
                </div>
                <div class="layui-form-mid layui-word-aux" style="margin-left:10px;">是否显示在领券中心和商品详情页</div>
            </div>
            <div class="layui-form-item" style="{if $info && $info['tolist']==0}display:none{/if}" id="showtj">
                <label class="layui-form-label">显示条件：</label>
                <div class="layui-input-inline" style="width:600px">
                    <input type="checkbox" name="info[showtj][]" value="-1" title="所有人" {if !$info || in_array('-1',$info['showtj'])}checked{/if}/>
                    <input type="checkbox" name="info[showtj][]" value="0" title="关注用户" {if $info && in_array('0',$info['showtj'])}checked{/if}/>
                    {foreach $memberlevel as $v}
                        <input type="checkbox" name="info[showtj][]" value="{$v.rank}" title="{$v.level_name}" {if $info && in_array($v['rank'],$info['showtj'])}checked{/if}/>
                    {/foreach}
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付后赠送：</label>
                <div class="layui-input-inline">
                    <input type="radio" name="info[paygive]" value="0" {if !$info || $info['paygive']==0}checked{/if} title="关闭" lay-filter="paygive"/>
                    <input type="radio" name="info[paygive]" value="1" {if $info && $info['paygive']==1}checked{/if} title="开启" lay-filter="paygive"/>
                </div>
                <div class="layui-form-mid layui-word-aux" style="margin-left:10px;">支付成功后自动发放优惠券，退款不扣除</div>
            </div>
            <div style="{if !$info || $info['paygive']==0}display:none{/if}" id="paygiveset">
                <div class="layui-form-item">
                    <label class="layui-form-label">支付金额范围：</label>
                    <div class="layui-input-inline" style="width:100px">
                        <input type="text" name="info[paygive_minprice]" value="{$info.paygive_minprice|default=0}" class="layui-input">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline" style="width:100px">
                        <input type="text" name="info[paygive_maxprice]" value="{$info.paygive_maxprice|default=9999}" class="layui-input">
                    </div>
                    <div class="layui-form-mid">元</div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">购买商品赠送：</label>
                <div class="layui-input-inline" style="width:500px">
                    <input type="radio" name="info[buyprogive]" value="0" {if !$info || $info['buyprogive']==0}checked{/if} title="关闭" lay-filter="buyprogive"/>
                    <input type="radio" name="info[buyprogive]" value="1" {if $info && $info['buyprogive']==1}checked{/if} title="开启" lay-filter="buyprogive"/>
                </div>
                <div class="buyprogive" style="clear:both;{if !$info || $info['buyprogive']!=1}display:none{/if}">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-inline" style="width:auto">
                        <table  class="layui-table" style="width:500px">
                            <thead>
                            <tr>
                                <th>商品ID</th>
                                <th>商品名称</th>
                                <th>赠送数量</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            {foreach $productdata2 as $k=>$ff}
                            <tr class="productlisttr2"><td>{$ff.goods_id}</td><td>{$ff.goods_name}</td><td><input name="info[buypro_give_num][]" type="text" value="{$info.buypro_give_num[$k]}" class="layui-input"></td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delProduct2(this,{$ff.id})">删除</button></td></tr>
                            {/foreach}
                            <tr id="productaddtr2">
                                <td colspan="4"><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="showChooseProduct2()">添加</button></td>
                            </tr>
                        </table>
                        <input type="hidden" name="info[buyproids]" value="{$info.buyproids}"/>
                    </div>
                </div>
                <script>
                    var chooseProductLayer2;
                    function showChooseProduct2(){
                        chooseProductLayer2 = layer.open({type:2,title:'选择商品',content:"{:url('Goods/choosegoods',['type'=>2])}",area:['1000px','600px'],shadeClose:true});
                    }
                    function choosepro2(res){
                        var product = res
                        layer.close(chooseProductLayer2);
                        var buyproids = [];
                        var isadd = 0;
                        $('.productlisttr2').each(function(){
                            var thisfid = $(this).find('td:eq(0)').html();
                            if(thisfid == product.id){
                                isadd = 1
                                dialog('该商品已添加过了');
                            }
                            buyproids.push(thisfid)
                        })
                        console.log(isadd);
                        if(isadd == 0){
                            buyproids.push(product.goods_id)
                            $("input[name='info[buyproids]']").val(buyproids.join(','));
                            $('#productaddtr2').before('<tr class="productlisttr2"><td>'+product.goods_id+'</td><td>'+product.goods_name+'</td><td><input name="info[buypro_give_num][]" type="text" value="1" class="layui-input"></td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-primary" onclick="delProduct2(this,'+product.goods_id+')">删除</button></td></tr>');
                        }
                        console.log(buyproids)
                    }
                    function delProduct2(obj,fid){
                        $(obj).parent().parent().remove();
                        var buyproids = [];
                        $('.productlisttr2').each(function(){
                            buyproids.push($(this).find('td:eq(0)').html())
                        })
                        $("input[name='info[buyproids]']").val(buyproids.join(','));
                    }
                </script>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">确认</button>
                </div>
            </div>
        </form>
</div>
</div>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;

        // 自定义验证规则
        form.verify({
            pass: [
                /^[\S]{6,12}$/,
                '密码必须6到12位，且不能出现空格'
            ]
        });

        // 指定开关事件
        // form.on('switch(switchTest)', function(data){
        //     layer.msg('开关 checked：'+ (this.checked ? 'true' : 'false'), {
        //         offset: '6px'
        //     });
        //     layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是 ON|OFF', data.othis)
        // });

        // 提交事件
        form.on('submit(demo1)', function(data){
            console.log(data);
            var index = layer.load();
            layer.close(index);

            var field = data.field; // 获取表单字段值
            $.ajax({
                type: "POST",
                url: "{:url('save')}",
                data: field,
                success: function(res){
                    console.log(res);
                    if(res.status){
                        layer.msg('提交成功');
                    }else{
                        layer.msg(res.msg);
                    }
                    setTimeout(function(){
                        parent.layer.closeAll();
                        parent.tableIns.reload()
                    },1000)
                }
            });
            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '#date'
        });


    });
</script>
<script>
    layui.form.on('radio(coupon_type)',function(data){
        if(data.value==1){
            $('.coupon_type1').show();
            $('.coupon_type1_1').show();
            $('.coupon_type1_10').hide();
            $('.coupon_yxqtime').show();
        }
        if(data.value==2){
            $('.coupon_type1').show();
            $('.coupon_type1_10').show();
            $('.coupon_type1_1').hide();
            $('.coupon_yxqtime').show();
        }
    });
    layui.form.on('radio(yxqtype)',function(data){
        $('.yxqtimeitem').hide();
        $('#yxqtimeitem_'+data.value).show();
    })

    layui.form.on('radio(paygive)',function(data){
        if(data.value==1){
            $('#paygiveset').show();
        }else{
            $('#paygiveset').hide();
        }
    })
    layui.form.on('radio(fwtype)',function(data){
        $('.fwtypeExtend').hide();
        $('.fwtype' + data.value).show();
    })

    layui.form.on('radio(buyprogive)',function(data){
        if(data.value==1){
            $('.buyprogive').show();
        }else{
            $('.buyprogive').hide();
        }
    })
    layui.form.on('radio(showtj)',function(data){
        if(data.value==1){
            $('#showtj').show();
        }else{
            $('#showtj').hide();
        }
    })
    layui.laydate.render({
        elem: '#starttime'
        ,type: 'datetime'
        ,range: false,
        trigger: 'click'
    });
    layui.laydate.render({
        elem: '#endtime'
        ,type: 'datetime'
        ,range: false,
        trigger: 'click'
    });
    layui.laydate.render({
        elem: '#yxqtime'
        ,type: 'datetime'
        ,range: '~',
        trigger: 'click'
    });

    // layui.form.on('submit(formsubmit)', function(obj){
    //     var field = obj.field
    //     var index = layer.load();
    //     $.post("{:url('save')}",field,function(data){
    //         layer.close(index);
    //         dialog(data.msg,data.status);
    //         if(data.status == 1){
    //             setTimeout(function(){
    //                 parent.layer.closeAll();
    //                 parent.tableIns.reload()
    //             },1000)
    //         }
    //     })
    // })
</script>
</body>
</html>